extends Node
## 事件总线


## 观察者
var _observers:Dictionary = {}


## 订阅
func subscribe(event_name:String, callable:Callable)->void:
	if not event_name or event_name.is_empty():
		return
	if not callable:
		return
	if not event_name in _observers:
		_observers[event_name] = []
	_observers[event_name].append(callable)
	

## 取消订阅
func unsubscribe(event_name:String, callable:Callable)->void:
	if _observers.is_empty() or not (event_name in _observers):
		return
	var callables:Array = _observers[event_name]
	callables = callables.filter(func(c): return c!=callable)
	if callables.is_empty():
		_observers.erase(event_name)
	else:
		_observers[event_name] = callables


## 发布
func publish(event_name:String)->void:
	if not event_name in _observers:
		return
	for callable:Callable in _observers[event_name]:
		if not callable:
			continue
		callable.call()
